package com.dexing.od1;

import java.util.Scanner;

public class D2082Excel {
    public static void main(String[] args) {
        // 处理输入
        Scanner in = new Scanner(System.in);
        String[] params = in.nextLine().split(" ");
        int rows = Integer.parseInt(params[0]);
        int cols = Integer.parseInt(params[1]);

        String[][] matrix = new String[rows][cols];
        for (int i=0;i<rows;i++) {
            matrix[i] = in.nextLine().split(" ");
        }

        // 先将表达式都转为数字字符串
        for (int i=0;i<rows;i++) {
            for (int j=0;j<cols;j++) {
                if (matrix[i][j].charAt(0) == '=') {
                    if (matrix[i][j].contains("+")) {
                        String[] op = matrix[i][j].split("\\+");
                        String op1 = op[0];
                        String op2 = op[1];
                        matrix[i][j] = String.valueOf(Integer.parseInt(matrix[Integer.parseInt(op1.substring(2)) - 1][(int)op1.charAt(1)-65]) + Integer.parseInt(matrix[Integer.parseInt(op2.substring(1))-1][(int)op2.charAt(0)-65]));
                    } else if (matrix[i][j].contains("-")) {
                        String[] op = matrix[i][j].split("-");
                        String op1 = op[0];
                        String op2 = op[1];
                        matrix[i][j] = String.valueOf(Integer.parseInt(matrix[Integer.parseInt(op1.substring(2)) - 1][(int)op1.charAt(1)-65]) - Integer.parseInt(matrix[Integer.parseInt(op2.substring(1))-1][(int)op2.charAt(0)-65]));
                    } else{
                        matrix[i][j] = String.valueOf(Integer.parseInt(matrix[Integer.parseInt(matrix[i][j].substring(2)) - 1][(int)matrix[i][j].charAt(1)-65]));
                    }
                }
            }
        }

        //输出表达式解析
        String[] output = in.nextLine().split(":");

        int res = 0;
        for (int i=Integer.parseInt(output[0].substring(1))-1;i<Integer.parseInt(output[1].substring(1));i++) {
            for (int j=(int)output[0].charAt(0)-65;j<(int)output[1].charAt(0)-65+1;j++) {
                res += Integer.parseInt(matrix[i][j]);
            }
        }

        System.out.print(res);

    }
}
